package org.mq.mymq.broker.utils;

import org.mq.mymq.broker.cache.CommonCache;
import org.mq.mymq.common.constants.BrokerConstants;

/**
 * @ClassName CommitLogFileNameUtils
 * @Description 根据规则生成CommitLog文件名
 * @Author jiarong_ye
 * @Date 2025/6/15 22:30
 * @Version 1.0
 */
public class LogFileNameUtil {

    /**
     * 构建第一份commitLog文件
     *
     * @return
     */
    public static String buildFirstCommitLogName() {
        return "00000000";
    }

    /**
     * 构建第一份ConsumeQueue文件名称
     *
     * @return
     */
    public static String buildFirstConsumeQueueName() {
        return "00000000";
    }

    /**
     * 返回新commitLog文件路徑
     *
     * @param topicName 主題名稱
     * @param commitLogFileName 文件名称
     * @return
     */
    public static String buildCommitLogFilePath(String topicName, String commitLogFileName) {
        return CommonCache.getGlobalProperties().getMyMqHome()
                + BrokerConstants.BASE_COMMIT_LOG_PATH
                + topicName
                + BrokerConstants.SPLIT
                + commitLogFileName;
    }

    /**
     * 构建commitLog所在目录的基本路径
     *
     * @param topicName 主題名稱
     * @return
     */
    public static String buildCommitLogBasePath(String topicName) {
        return CommonCache.getGlobalProperties().getMyMqHome()
                + BrokerConstants.BASE_COMMIT_LOG_PATH
                + topicName;
    }

    /**
     * 构建consumeQueue所在目录的基本路径
     *
     * @param topicName 主題名稱
     * @return
     */
    public static String buildConsumeQueueBasePath(String topicName) {
        return CommonCache.getGlobalProperties().getMyMqHome()
                + BrokerConstants.BASE_CONSUMER_QUEUE_PATH
                + topicName;
    }

    /**
     * 构建consumeQueue文件路径
     *
     * @param topicName
     * @param queueId
     * @param consumeQueueFileName
     * @return
     */
    public static String buildConsumeQueueFilePath(String topicName, int queueId, String consumeQueueFileName) {
        return CommonCache.getGlobalProperties().getMyMqHome()
                + BrokerConstants.BASE_CONSUMER_QUEUE_PATH
                + topicName
                + BrokerConstants.SPLIT
                + queueId
                + BrokerConstants.SPLIT
                + consumeQueueFileName;
    }

    public static String incrConsumeQueueFileName(String oldFileName) {
        return incrCommitLogName(oldFileName);
    }

    /**
     * 根据传入commitLog名称，生成下一个commitLog名称
     *
     * @param lastCommitLogName 上一个commitLog名称
     * @return
     */
    public static String incrCommitLogName(String lastCommitLogName) {
        if (lastCommitLogName.length() != 8) {
            throw new IllegalArgumentException("commitLogFileName must be 8 characters");
        }
        Long fileIndex = Long.parseLong(lastCommitLogName);
        fileIndex++;
        String newCommitLogName = String.valueOf(fileIndex);
        int needFullLen = 8- newCommitLogName.length();
        if (needFullLen < 0) {
            throw new IllegalArgumentException("commitLogFileName needs to be at least 8 characters");
        }
        StringBuffer stb = new StringBuffer();
        for (int i = 0; i < needFullLen; i++) {
            stb.append('0');
        }
        stb.append(newCommitLogName);
        return stb.toString();
    }

    public static void main(String[] args) {
        String firstCommitLogName = buildFirstCommitLogName();
        for (int i = 0; i < 99999999; i++) {
            firstCommitLogName = incrCommitLogName(firstCommitLogName);
        }
    }

}
